﻿namespace System.ServiceManager.Facade
{
    public static class SCSMSettings
    {
        private static IncidentSettings incidentSettings;
        private static ActivitySettings activitySettings;
        private static ChangeManagementSettings changeManagementSettings;


        public static IncidentSettings IncidentSettings
        {
            get
            {


                if (incidentSettings == null)
                    GetIncidentSettings();

                return incidentSettings;
            }
        }

        public static ActivitySettings ActivitySettings
        {
            get
            {
                if (activitySettings == null)
                    GetActivitySettings();
                return activitySettings;
            }
        }

        public static ChangeManagementSettings ChangeManagementSettings
        {
            get
            {
                if (changeManagementSettings == null)
                    GetChangeManagementSettings();

                return changeManagementSettings;
            }
        }

        private static void GetActivitySettings()
        {

            var @class = SCSMServer.GetSingletonObject(EnterpriseManagementClasses.System_GlobalSetting_ActivitySettingsInfo.GetManagementPackClass());
            if (@class == null)
            {
                throw new InvalidOperationException("Could not retrieve Default Incident Settings");
            }

            activitySettings = new ActivitySettings
                                   {
                                       ManualActivityIDPrefix =
                                           @class[null, "SystemWorkItemActivityManualActivityIdPrefix"].Value.
                                           ToString(),
                                       ReviewActivityIDPrefix =
                                           @class[null, "SystemWorkItemActivityReviewActivityIdPrefix"].Value.
                                           ToString()
                                   };


        }

        private static void GetChangeManagementSettings()
        {
            var @class = SCSMServer.GetSingletonObject(EnterpriseManagementClasses.System_GlobalSetting_ChangeSettingsInfo.GetManagementPackClass());

            changeManagementSettings = new ChangeManagementSettings
                                           {
                                               ChangeRequestIDPrefix =
                                                   @class[null, "SystemWorkItemChangeRequestIdPrefix"].Value.
                                                   ToString(),
                                               MaxFileAttachmentsCount =
                                                   Convert.ToInt32(@class[null, "MaxFileAttachmentsCount"].Value),
                                               MaxFileAttachmentSizeKB =
                                                   Convert.ToInt32(@class[null, "MaxFileAttachmentSizeinKB"].Value)
                                           };

        }


        private static void GetIncidentSettings()
        {

            var @class = SCSMServer.GetSingletonObject(EnterpriseManagementClasses.System_WorkItem_Incident_GeneralSettingInfo.GetManagementPackClass());
            if (@class == null)
            {
                throw new InvalidOperationException("Could not retrieve Default Incident Settings");
            }

            incidentSettings = new IncidentSettings
                                       {
                                           MaxAttachments = Convert.ToInt32(@class[null, "MaxAttachments"].Value),
                                           MaxAttachmentSize = Convert.ToInt32(@class[null, "MaxAttachmentSize"].Value),
                                           PrefixForIdentifier = @class[null, "PrefixForId"].Value.ToString(),
                                           DataRetentionTimeUnit =
                                               Convert.ToInt32(@class[null, "DataRetentionTimeUnit"].Value),
                                           DataRetentionTime = Convert.ToInt32(@class[null, "DataRetentionTime"].Value),
                                           PriorityMatrix = @class[null, "PriorityMatrix"].Value.ToString(),
                                           ResolutionMappings = @class[null, "ResolutionMappings"].Value.ToString()
                                           
                                       };
            // TODO do not know why this is null
            //incidentSettings.DefaultSupportGroup = settings[null, "DefaultSupportGroup"].Value.ToString();

        }

    }
}
